package com.wing.cabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.cabinet.dao.CabinetCargoSpaceDao;
import com.wing.cabinet.model.entity.*;
import com.wing.cabinet.model.request.CabinetCargoSpaceForm;
import com.wing.cabinet.model.request.UseInventoryInoutDetailForm;
import com.wing.cabinet.model.response.CabinetCargoSpaceVO;
import com.wing.cabinet.model.response.StorageInventoryUncheckVO;
import com.wing.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收刀柜货位-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class CabinetCargoSpaceServiceImpl extends ServiceImpl<CabinetCargoSpaceDao, CabinetCargoSpace> implements CabinetCargoSpaceService {


    @Override
    public int add(CabinetCargoSpaceForm cabinetCargoSpaceForm) {

        //校验

        CabinetCargoSpace cabinetCargoSpace =new CabinetCargoSpace();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(CabinetCargoSpace.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(cabinetCargoSpaceForm, cabinetCargoSpace, copyOptions);

        return baseMapper.insert(cabinetCargoSpace);
    }

    @Override
    public List<CabinetCargoSpaceVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<CabinetCargoSpaceVO> pageList4app(Page page, Map<String, Object> params) {
        List<CabinetCargoSpaceVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<CabinetCargoSpaceVO> pageList4MainCab(Page page, Map<String, Object> params) {
        List<CabinetCargoSpaceVO> list = baseMapper.list4MainCab(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<CabinetCargoSpaceVO> pageList4Cab(Page page, Map<String, Object> params) {
        List<CabinetCargoSpaceVO> list = baseMapper.list4Cab(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<CabinetCargoSpaceVO> pageList4Load(Page page, Map<String, Object> params) {
        List<CabinetCargoSpaceVO> list = baseMapper.list4Load(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<CabinetCargoSpaceVO> pageList4pc(Page page, Map<String, Object> params) {
        List<CabinetCargoSpaceVO> list = baseMapper.list4pc(page, params);
        for(CabinetCargoSpaceVO item :list){
            if(StringUtils.isNotBlank(item.getPrincipal())){
                item.setSpaceNature(CabinetCargoSpace.SpaceNature.personal);
            }else if(item.getSpaceProductType()!=null && item.getSpaceProductType() != 0){
                item.setSpaceNature(CabinetCargoSpace.SpaceNature.specialClass);
            }else if(StringUtils.isNotBlank(item.getSkuCode())){
                item.setSpaceNature(CabinetCargoSpace.SpaceNature.sku);
            }else{
                item.setSpaceNature(CabinetCargoSpace.SpaceNature.common);
            }
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public CabinetCargoSpaceVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public List<CabinetCargoSpaceVO> getUsableSpace(String cabCode,Integer businessType, Integer quantity, String skuCode,Integer spaceNature,Long principal,  Long spaceProductType) {
        String[] cabCodes=cabCode.split(",");
        return baseMapper.getUsableSpace(cabCodes,businessType,quantity,skuCode,spaceNature,principal,spaceProductType);
    }

    @Override
    public List<CabinetCargoSpaceVO> getUsedSpace(String cabCode,Integer businessType, Integer quantity, String skuCode,String spaceNature,Integer spaceNatureValue,Long principal,  Long spaceProductType,Long creator) {
        String[] cabCodes=cabCode.split(",");
        return baseMapper.getUsedSpace(cabCodes,businessType,quantity,skuCode,spaceNature,spaceNatureValue,principal,spaceProductType,creator);
    }

    @Override
    public List<CabinetCargoSpaceVO> getEmptySpaces(String cabCode, String spaceNature,Integer spaceNatureValue,Long principal,  Long spaceProductType) {
        String[] cabCodes=cabCode.split(",");
        return baseMapper.getEmptySpaces(cabCodes,spaceNature,spaceNatureValue,principal,spaceProductType);
    }


    @Override
    public List<CabinetCargoSpaceVO> getRecycleRepairSpaces(String cabCode, Integer spaceNature,Long principal,  Long spaceProductType) {
        String[] cabCodes=cabCode.split(",");
        return baseMapper.getRecycleRepairSpaces(cabCodes,spaceNature,principal,spaceProductType);
    }

    @Override
    public List<CabinetCargoSpaceVO> getUsedSpace(String cabCode, Integer spaceType, Long principal, Long spaceProductType, String skuCode, String skuCodeCurrent, Integer quantity,Integer businessType) {
        String[] cabCodes=cabCode.split(",");
        return baseMapper.getUsedSpace2(cabCodes,spaceType,principal,spaceProductType,skuCode,skuCodeCurrent,quantity,businessType);
    }

    @Override
    public List<CabinetCargoSpaceVO> getEmptySpaces(String cabCode, Integer spaceType, Long principal, Long spaceProductType, String skuCode) {
        String[] cabCodes=cabCode.split(",");
        return baseMapper.getEmptySpaces2(cabCodes,spaceType,principal,spaceProductType,skuCode);
    }

    @Override
    public List<CabinetCargoSpaceVO> getSpaceByCabCode(String cabCode) {
        String[] cabCodes=cabCode.split(",");
        return baseMapper.getSpaceByCabCode(cabCodes);
    }

    @Override
    public List<CabinetCargoSpaceVO> getUserSpaceByCabCode(String cabCode,List<Integer> spaceTyeps,Long userId) {
        String[] cabCodes=cabCode.split(",");
        return baseMapper.getUserSpaceByCabCode(cabCodes,spaceTyeps,userId);
    }

    @Override
    public Page<CabinetCargoSpaceVO> cellListLoad(Page page, Map<String, Object> params) {
        List<CabinetCargoSpaceVO> list = baseMapper.cellListLoad(page, params);
        page.setRecords(list);
        return page;
    }
}
